//
//  ColorUtil.swift
//  TYYSDK
//
//  Created by SunXianHua on 2018/8/9.
//  Copyright © 2018年 TYY. All rights reserved.
//

import UIKit

extension UIColor {
    /**
     通过颜色获取图片
     
     - parameter color: UIColor
     
     - returns: imageWithCIColor
     */
    static func imageWithUIColor(  _ color:UIColor) -> UIImage
    {
        let rect:CGRect = CGRect(x: 0, y: 0, width: 1, height: 1);
        UIGraphicsBeginImageContext(rect.size);
        let context:CGContext = UIGraphicsGetCurrentContext()!;
        
        context.setFillColor(color.cgColor);
        context.fill(rect);
        
        let image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        return image!;
    }
    
    static func imageWithFloat(  _ red:CGFloat ,  green:CGFloat ,  blue:CGFloat,  alpha:CGFloat) -> UIImage
    {
        let color = set_Color(red, green: green, blue: blue, alpha: alpha);
        
        return imageWithUIColor(color);
    }
    
    static func imageWithFloat(  _ colorArray:[CGFloat]) -> UIImage
    {
        let color = colorFloat(colorArray);
        
        return imageWithUIColor(color);
    }
    
    /**
     使用进行UIColor初始化
     
     - parameter red:
     - parameter green:
     - parameter blue:
     - parameter alpha:
     
     - returns:
     */
    static func set_Color(  _ red:CGFloat ,  green:CGFloat ,  blue:CGFloat,  alpha:CGFloat) -> UIColor
    {
        return UIColor.init(red:red/255.0, green: green/255.0, blue: blue/255.0, alpha: alpha);
    }
    
    
    static func colorFloat(  _ color:[CGFloat]) -> UIColor
    {
        if color.count < 3
        {
            return UIColor.clear;
        }
        
        var alpha:CGFloat = 1;
        if color.count == 4
        {
            alpha = color[3];
        }
        
        return UIColor.init(red: color[0], green: color[1], blue: color[2], alpha: alpha);
    }
    
    static func colorUIHex(_ hexStr:String) -> UIColor
    {
        if hexStr.isEmpty
        {
            return UIColor.clear;
        }
        
        let uiHex = hexStr.replacingOccurrences(of: "#", with: "")
        
        if uiHex.count == 8 {
            return colorUIHexARGB(uiHex)
        }
        
        if uiHex.count == 6 {
            return colorUIHexRGB(uiHex)
        }
        
        return UIColor.clear;
    }
    
    
    static func colorUIHexARGB(_ hexStr:String) -> UIColor
    {
        if hexStr.isEmpty || hexStr.count != 8
        {
            return UIColor.clear;
        }
        
        
        
        let index = hexStr.index(hexStr.startIndex, offsetBy: 2);
        let alphaStr = "0x\(String(hexStr[..<index]))";
        
        let redIndex = hexStr.index(hexStr.startIndex, offsetBy: 4);
        let redStr = "0x\(String(hexStr[index..<redIndex]))";
        
        let greenIndex = hexStr.index(hexStr.startIndex, offsetBy: 6);
        let greenStr = "0x\(String(hexStr[redIndex..<greenIndex]))";
        
        let blueIndex = hexStr.index(hexStr.startIndex, offsetBy: 8);
        let blueStr = "0x\(String(hexStr[greenIndex..<blueIndex]))";
        
        var alpha:CUnsignedInt = 0;
        var red:CUnsignedInt = 0;
        var green:CUnsignedInt = 0;
        var blue:CUnsignedInt = 0;
        
        Scanner.init(string: alphaStr).scanHexInt32(&alpha);
        Scanner.init(string: redStr).scanHexInt32(&red);
        Scanner.init(string: greenStr).scanHexInt32(&green);
        Scanner.init(string: blueStr).scanHexInt32(&blue);
        
        return UIColor.init(red:CGFloat(red)/255.0, green: CGFloat(green)/255.0, blue: CGFloat(blue)/255.0, alpha: CGFloat(alpha)/255.0);
    }
    
    static func colorUIHexRGB(  _ hexStr:String) -> UIColor
    {
        if hexStr.isEmpty || hexStr.count != 6
        {
            return UIColor.clear;
        }
        
        
        let redIndex = hexStr.index(hexStr.startIndex, offsetBy: 2);
        let redStr = "0x\(String(hexStr[..<redIndex]))";
        
        let greenIndex = hexStr.index(hexStr.startIndex, offsetBy: 4);
        let greenStr = "0x\(String(hexStr[redIndex..<greenIndex]))";
        
        let blueIndex = hexStr.index(hexStr.startIndex, offsetBy: 6);
        let blueStr = "0x\(String(hexStr[greenIndex..<blueIndex]))";
        
        var red:CUnsignedInt = 0;
        var green:CUnsignedInt = 0;
        var blue:CUnsignedInt = 0;
        
        Scanner.init(string: redStr).scanHexInt32(&red);
        Scanner.init(string: greenStr).scanHexInt32(&green);
        Scanner.init(string: blueStr).scanHexInt32(&blue);
        
        return UIColor.init(red:CGFloat(red)/255.0, green: CGFloat(green)/255.0, blue: CGFloat(blue)/255.0, alpha: 1);
    }
    
    /// 颜色二值化
    ///
    /// - Parameter color:
    /// - Returns: 只返回黑白2种颜色
    static func blackWhite(_ color:UIColor) -> UIColor
    {
        if let colors = color.cgColor.components
        {
            let r = colors[0]*0.3
            let g = colors[1]*0.59
            let b = colors[2]*0.11
            
            if r+g+b < 0.5 {
                return UIColor.white
            }
        }
        return UIColor.black
    }
    
    
}
